﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Data;
using System.Configuration;

public partial class partner_client_add : System.Web.UI.Page
{
    string c_id = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["email"] == null)
        {
            Response.Redirect("sessiont.aspx");
        }
        Label2.Text = Session["email"].ToString();

    }
    //[WebMethod]

    //public static string InsertMethod(string f_name, string m_name, string l_name, string d_o_b, string occu, string h_no, string city, string street, string ssn, string zip_code, string country, string email, string ph_no, string cat, string c_cat, string cat1)
    //{


    //        SqlConnection conn = null;
    //        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        {

    //            SqlCommand cmd = new SqlCommand("Insert into client_add1 values('" + f_name + "', '" + m_name + "', '" + l_name + "','" + d_o_b + "','" + occu + "','" + h_no + "','" + city + "','" + street + "','" + ssn + "','" + zip_code + "','" + country + "','" + email + "','" + ph_no + "','" + cat + "','" + c_cat + "','" + cat1 + "')", conn);
    //            {
    //                conn.Open();
    //                cmd.ExecuteNonQuery();
    //                return "True";
    //            }
    //        }
    //    }



    protected void Cancel_Click(object sender, System.EventArgs e)
    {
        //btnSave_Student.Visible = true;
    }




    protected void Save_Click(object sender, System.EventArgs e)
    {

        string cat = null;
        cat = DropDownList1.SelectedValue;
        SqlConnection conn = null;
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
        conn.Open();
        SqlCommand scmd = new SqlCommand("SELECT * FROM client_add1 where email='" + txtemail.Text + "'", conn);

        SqlDataReader dr = scmd.ExecuteReader();
        if (dr.Read())
        {

            Label1.Visible = true;


        }


        else
        {
            dr.Close();


            //SqlConnection conn = null;

            //conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);



            if (cat.Equals("Yes"))
            {

                Random r1 = new Random();
                int num = r1.Next(0, 99999999); // generate random number
                string number = string.Format("{0:D8}", num);

                Random r2 = new Random();
                int num2 = r2.Next(0, 26); // generate random letter
                char letter = (char)('A' + num2);


                c_id = letter + number; // join both as ID 


                SqlCommand cmd = new SqlCommand("Insert into client_add1 values(@c_id,@f_name,@m_name,@l_name,@d_o_b,@occu,@h_no,@city,@street,@ssn,@zip_code,@country,@email,@ph_no,@cat,@c_cat,@cat1,@wants,@adding_date,@adding_by,@status )", conn);
                cmd.Parameters.AddWithValue("@c_id", c_id);
                cmd.Parameters.AddWithValue("@f_name", txtfname1.Text);
                cmd.Parameters.AddWithValue("@m_name", txtmname.Text);
                cmd.Parameters.AddWithValue("@l_name", txtlname1.Text);
                cmd.Parameters.AddWithValue("@d_o_b", txtdob1.Text);
                cmd.Parameters.AddWithValue("@occu", txtoccupation.Text);
                cmd.Parameters.AddWithValue("@h_no", txthno.Text);
                cmd.Parameters.AddWithValue("@city", txtcity.Text);
                cmd.Parameters.AddWithValue("@street", txtstreet.Text);
                cmd.Parameters.AddWithValue("@ssn", txtssn.Text);
                cmd.Parameters.AddWithValue("@zip_code", txtzip.Text);
                cmd.Parameters.AddWithValue("@country", txtcountry.Text);
                cmd.Parameters.AddWithValue("@email", txtemail.Text);
                cmd.Parameters.AddWithValue("@ph_no", txtphno.Text);
                cmd.Parameters.AddWithValue("@cat", ddlcategory.SelectedValue);
                cmd.Parameters.AddWithValue("@c_cat", DropDownList1.SelectedValue);
                cmd.Parameters.AddWithValue("@cat1", txtcatname.Text);
                cmd.Parameters.AddWithValue("@wants", DropDownList4.SelectedValue);
                cmd.Parameters.AddWithValue("@adding_date", System.DateTime.Now);
                cmd.Parameters.AddWithValue("@adding_by", Session["email"]);
                cmd.Parameters.AddWithValue("@status", "0");
                cmd.ExecuteNonQuery();
                Session["c_id"] = c_id;
                Response.Redirect("partner_client_add_confirmation.aspx");

            }
            else
            {

                Random r1 = new Random();
                int num = r1.Next(0, 99999999); // generate random number
                string number = string.Format("{0:D8}", num);

                Random r2 = new Random();
                int num2 = r2.Next(0, 26); // generate random letter
                char letter = (char)('A' + num2);


                c_id = letter + number; // join both as ID 
                SqlCommand cmd = new SqlCommand("Insert into client_add1 values(@c_id,@f_name,@m_name,@l_name,@d_o_b,@occu,@h_no,@city,@street,@ssn,@zip_code,@country,@email,@ph_no,@cat,@c_cat,@cat1,@wants,@adding_date,@adding_by,@status )", conn);
                cmd.Parameters.AddWithValue("@c_id", c_id);
                cmd.Parameters.AddWithValue("@f_name", txtfname1.Text);
                cmd.Parameters.AddWithValue("@m_name", txtmname.Text);
                cmd.Parameters.AddWithValue("@l_name", txtlname1.Text);
                cmd.Parameters.AddWithValue("@d_o_b", txtdob1.Text);
                cmd.Parameters.AddWithValue("@occu", txtoccupation.Text);
                cmd.Parameters.AddWithValue("@h_no", txthno.Text);
                cmd.Parameters.AddWithValue("@city", txtcity.Text);
                cmd.Parameters.AddWithValue("@street", txtstreet.Text);
                cmd.Parameters.AddWithValue("@ssn", txtssn.Text);
                cmd.Parameters.AddWithValue("@zip_code", txtzip.Text);
                cmd.Parameters.AddWithValue("@country", txtcountry.Text);
                cmd.Parameters.AddWithValue("@email", txtemail.Text);
                cmd.Parameters.AddWithValue("@ph_no", txtphno.Text);
                cmd.Parameters.AddWithValue("@cat", ddlcategory.SelectedValue);
                cmd.Parameters.AddWithValue("@c_cat", DropDownList1.SelectedValue);
                cmd.Parameters.AddWithValue("@cat1", "no custom");
                cmd.Parameters.AddWithValue("@wants", DropDownList4.SelectedValue);
                cmd.Parameters.AddWithValue("@adding_date", System.DateTime.Now);
                cmd.Parameters.AddWithValue("@adding_by", Session["email"]);
                cmd.Parameters.AddWithValue("@status", "0");
                cmd.ExecuteNonQuery();
                Session["c_id"] = c_id;
                Response.Redirect("partner_client_add_confirmation.aspx");

            }
        }
        conn.Close();


    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("../partner.aspx");
    }
}